apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  labels:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: {{ prometheus_tag | replace("v", "") }}
  name: k8s
  namespace: kubesphere-monitoring-system
spec:
{% if monitoring.affinity is defined and monitoring.affinity is not none %}
  affinity:
    {{  monitoring.affinity | to_nice_yaml(indent=2) | indent(4) }}
{% elif affinity is defined and affinity is not none %}
  affinity:
    {{ affinity | to_nice_yaml(indent=2) | indent(4) }}
{% else %}
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - preference:
          matchExpressions:
          - key: node-role.kubernetes.io/monitoring
            operator: Exists
        weight: 100
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - podAffinityTerm:
          labelSelector:
            matchLabels:
              app.kubernetes.io/component: prometheus
              app.kubernetes.io/instance: k8s
              app.kubernetes.io/name: prometheus
              app.kubernetes.io/part-of: kube-prometheus
          namespaces:
          - kubesphere-monitoring-system
          topologyKey: kubernetes.io/hostname
        weight: 100
{% endif %}
  alerting:
    alertmanagers:
    - name: alertmanager-main
      namespace: kubesphere-monitoring-system
      port: web
  evaluationInterval: 1m
  image: {{ prometheus_repo }}:{{ prometheus_tag }}
  podMetadata:
    labels:
      app.kubernetes.io/component: prometheus
      app.kubernetes.io/instance: k8s
      app.kubernetes.io/name: prometheus
      app.kubernetes.io/part-of: kube-prometheus
      app.kubernetes.io/version: {{ prometheus_tag | replace("v", "") }}
{% if monitoring.nodeSelector is defined and monitoring.nodeSelector is not none %}
  nodeSelector:
    {{ monitoring.nodeSelector | to_nice_yaml(indent=2) | indent(4) }}
{% elif nodeSelector is defined and nodeSelector is not none %}
  nodeSelector:
    {{ nodeSelector | to_nice_yaml(indent=2) | indent(4) }}
{% else %}
  nodeSelector:
    kubernetes.io/os: linux
{% endif %}
{% if monitoring.tolerations is defined and monitoring.tolerations is not none %}
  tolerations:
    {{ monitoring.tolerations | to_nice_yaml(indent=2) | indent(4) }}
{% elif tolerations is defined and tolerations is not none %}
  tolerations:
    {{ tolerations | to_nice_yaml(indent=2) | indent(4) }}
{% else %}
  tolerations:
  - effect: NoSchedule
    key: dedicated
    operator: Equal
    value: monitoring
{% endif %}
  podMonitorNamespaceSelector: {}
  podMonitorSelector: {}
  query:
    maxConcurrency: 1000
{% if nodeNum is defined and nodeNum < 3 %}
  replicas: {{ monitoring.prometheus.replicas | default(monitoring.prometheusReplicas) | default(1) }}
{% else %}
  replicas: {{ monitoring.prometheus.replicas | default(monitoring.prometheusReplicas) | default(2) }}
{% endif %}
  resources:
    limits:
      cpu: "{{ monitoring.prometheus.resources.limits.cpu | default(monitoring.prometheus.limits.cpu) | default("4") }}"
      memory: {{ monitoring.prometheus.resources.limits.memory | default(monitoring.prometheus.limits.memory) | default("16Gi") }}
    requests:
      cpu: {{ monitoring.prometheus.resources.requests.cpu | default(monitoring.prometheus.requests.cpu) | default("200m") }}
      memory: {{ monitoring.prometheus.resources.requests.memory | default(monitoring.prometheus.requests.memory) | default("400Mi") }}
  retention: {% if prometheus_retention_duration is defined %}{{ prometheus_retention_duration }}{% else %}7d{% endif %}

  ruleNamespaceSelector: {}
  ruleSelector:
    matchLabels:
      prometheus: k8s
      role: alert-rules
  scrapeInterval: 1m
{% if etcd.monitoring is defined and etcd.monitoring == true and etcd.tlsEnable == true %}
  secrets:
  - kube-etcd-client-certs
{% endif %}
  securityContext:
    fsGroup: 0
    runAsNonRoot: false
    runAsUser: 0
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  storage:
    volumeClaimTemplate:
      spec:
{% if monitoring.storageClass is defined and monitoring.storageClass != "" %}
        storageClassName: {{ monitoring.storageClass }}
{% elif persistence.storageClass is defined and persistence.storageClass != "" %}
        storageClassName: {{ persistence.storageClass }}
{% endif %}
        resources:
          requests:
            storage: {% if prometheus_pv_size is defined %}{{ prometheus_pv_size }}{% else %}{{ monitoring.prometheus.volumeSize | default(monitoring.prometheusVolumeSize) | default("20Gi") }}{% endif %}

  version: {{ prometheus_tag }}
